package cn.rongdao.jrkabao.dao;

import com.mifengkong.frtools.util.FRString;
import com.mifengkong.greendao.bean.VersionInfo;

import java.util.List;


/**
 * Created by jack on 2017/9/19
 */

public class VersionDao extends BaseDao<VersionInfo> {

    public static VersionDao getInstance() {
        return getPresent(VersionDao.class);
    }

    private VersionInfo getVersionInfo() {
        List<VersionInfo> versionInfoList = queryAll(VersionInfo.class);
        if (null != versionInfoList && versionInfoList.size() > 0) {
            return versionInfoList.get(0);
        }
        return null;
    }

    public boolean isNeedUpdate() {
        return null != getVersionInfo() && FRString.valueOf(getVersionInfo().getNeed_update()).equals("1");
    }

    public boolean isForceUpdate() {
        return null != getVersionInfo() && FRString.valueOf(getVersionInfo().getForce_update()).equals("1");
    }

    public String getDownLoadUrl() {
        if (null != getVersionInfo()) {
            return FRString.valueOf(getVersionInfo().getDownload_url());
        }
        return FRString.EMPTY_STRING;
    }

    public String getUpdateInfo() {
        if (null != getVersionInfo()) {
            return FRString.valueOf(getVersionInfo().getVersion_info());
        }
        return FRString.EMPTY_STRING;
    }

    public String getNewVersion() {
        if (null != getVersionInfo()) {
            return FRString.valueOf(getVersionInfo().getNew_version());
        }
        return FRString.EMPTY_STRING;
    }

    public String getVersionTitle() {
        if (null != getVersionInfo()) {
            return FRString.valueOf(getVersionInfo().getVersion_title());
        }
        return FRString.EMPTY_STRING;
    }

    public boolean updateVersionInfo(VersionInfo versionInfo) {
        if (null != versionInfo) {
            if (null != getVersionInfo()) {
                versionInfo.setId(getVersionInfo().getId());
                updateObject(versionInfo);
            } else {
                insertObject(versionInfo);
            }
            return true;
        }
        return false;
    }

    public void deleteVersionInfo() {
        if (!FRString.isEmpty(getDownLoadUrl())) {
            VersionInfo versionInfo = getVersionInfo();
            if (null != versionInfo) {
                deleteObject(versionInfo);
            }
        }
    }
}